<?php
defined('BY_JYA') or exit('error');
$return=array();
$plugin_name=$_RQ['plugin_name'];
$pid=pdo_getcolumn('core_plugins',array('name'=>$plugin_name),'id');
$item=pdo_get('core_open_plugin',array('pid'=>$pid,'opentype'=>'wxwork','uniacid'=>$_SESSION['uniacid'],'isplugin'=>0));
if($item['saastype']){
	$backurl=urlencode(custom_url('core/member.wxwork_auth//isget=1&plugin_name='.$plugin_name,true));
	$appid=pdo_getcolumn('core_account_wxwork',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['wxwork_id']),'appid');
	if($appid){
		$item['auth_url']='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$backurl.'&response_type=code&scope=snsapi_base&state=getcode#wechat_redirect';
	}
	$_SESSION['open_userid']='isget';
}else{
	$backurl=urlencode(custom_url('core/member.wxwork_auth//isget=1&plugin_name='.$plugin_name,true));
	$item['auth_url']='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$item['suite_id'].'&redirect_uri='.$backurl.'&response_type=code&scope=snsapi_base&state=getcode#wechat_redirect';
}
$code=$_RQ['code'];
if($code || $_RQ['isget']){
    if($item['saastype']){
        cfc('fans_api')->up_userinfo('base',$code);
    }else{
	    cfc('workfans_api')->upfans($item['id'],$code,$pid);
    }
}else{
	if($item['auth_url']){
	    if($item['saastype'] || !$_SESSION['openid']){
	        exi('','',$item['auth_url']);
	    }elseif(!$_SESSION['open_userid']){
	    	exi('','',$item['auth_url']);
	    }
	}
}
if((!$item['saastype'] && !$_SESSION['open_userid']) || ($item['saastype'] && !$_SESSION['openid']) || !$_SESSION['uid']){
	$return['item']=$item;
	exi($return);
}elseif($_SESSION['open_userid']=='isget'){
    exi($return);
}else{
	$url=custom_url($plugin_name.'/index/',true);
	exi('','',$url);
}